unit Unit12; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  DbCtrls, sqldb, db, Buttons, Unit1;

type

  { TForm12 }

  TForm12 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Datasource1: TDatasource;
    Datasource2: TDatasource;
    DBComboBox1: TDBComboBox;
    DBComboBox2: TDBComboBox;
    Edit1: TEdit;
    Edit10: TEdit;
    Edit11: TEdit;
    Edit12: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Label1: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    SQLQuery1: TSQLQuery;
    SQLQuery2: TSQLQuery;
    SQLQuery3: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Edit5KeyPress(Sender: TObject; var Key: char);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow;
  private
    { private declarations }
  public
    nombre : string;
    codigo : string;
    precio1 : string;
    precio2 : string;
    precio3 : string;
    precio4 : string;
  end; 

var
  Form12: TForm12; 

implementation

{ TForm12 }

procedure TForm12.FormShow;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  if SQLQuery2.Active = True then
    SQLQuery2.Close;
    
  SQLQuery1.Database := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
  SQLQuery1.SQL.Text := 'select fmp_nombre from familia_producto order by fmp_nombre';
  
  SQLQuery1.Open;
  
  DBComboBox1.DataField := 'fmp_nombre';
  DBComboBox1.Clear;
  
  while not SQLQuery1.EOF do
  begin
    DBComboBox1.Items.Add(SQLQuery1.Fields[0].AsString);
    SQLQuery1.Next;
  end;
  
  SQLQuery2.Database := Form1.PQConnection1;
  SQLQuery2.Transaction := Form1.SQLTransaction1;
  SQLQuery2.SQL.Text := 'select fab_nombre from fabricante order by fab_nombre';
  
  SQLQuery2.Open;
  
  DBComboBox2.DataField := 'fab_nombre';
  DBComboBox2.Clear;

  while not SQLQuery2.EOF do
  begin
    DBComboBox2.Items.Add(SQLQuery2.Fields[0].AsString);
    SQLQuery2.Next;
  end;
  
  SQLQuery3.Database := Form1.PQConnection1;
  SQLQuery3.Transaction := Form1.SQLTransaction1;

  Edit1.Text := '';
  Edit2.Text := '';
  DBComboBox1.Text := '';
  DBComboBox2.Text := '';
  Edit3.Text := '0';
  Edit6.Text := '0';
  Edit4.Text := '21.0';
  Edit7.Text := '0';
  Edit5.Text := '';
  Edit8.Text := '0';
  Edit9.Text := '0';
  Edit10.Text := '0';
  Edit11.Text := '0';
  Edit12.Text := '0';
  
  codigo := '';
  precio1 := '';
  precio2 := '';
  precio3 := '';
  precio4 := '';
  nombre := '';
  
  if (registro.tipo_impresor = 'Hasar P715F') or (registro.tipo_impresor = 'Hasar PR4F')
    or(registro.tipo_impresor = 'Epson TM2000') then
    Edit1.MaxLength := 18;
  
  Edit5.SetFocus;
end;

procedure TForm12.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm12.Edit5KeyPress(Sender: TObject; var Key: char);
begin
  if Key = chr(13) then
    Edit1.SetFocus;
end;

procedure TForm12.Button1Click(Sender: TObject);
var
  QueryString : String;
  
begin
  if(Edit1.Text <> '') and (DBComboBox1.Text <> '')
    and (DBComboBox2.Text <> '') and (Edit4.Text <> '')then
  begin
    if Edit5.Text = '' then
      QueryString := 'insert into producto(prd_nombre, prd_descripcion, prd_familia, prd_fabricante, '
        + 'prd_precio1, prd_stock_minimo, prd_stock_maximo, prd_iva, prd_impint, prd_precio2,'
        + ' prd_precio3, prd_precio4) values('''
        + Edit1.Text + ''', ''' + Edit2.Text + ''', (select fmp_cdg from familia_producto '
        + 'where fmp_nombre = ''' + DBComboBox1.Text + '''), '
        + '(select fab_cdg from fabricante where fab_nombre = '''
        + DBComboBox2.Text + '''), ' + Edit3.Text + ', ' + Edit6.Text + ', ' + Edit7.Text + ', '
        + Edit4.Text + ', ' + Edit9.Text + ', ' + Edit10.Text + ', ' + Edit11.Text + ', '
        + Edit12.Text + ')'
    else
      QueryString := 'insert into producto(prd_cdg, prd_nombre, prd_descripcion, prd_familia, '
        + 'prd_fabricante, prd_precio1, prd_stock_minimo, prd_stock_maximo, prd_iva, prd_impint, '
        + 'prd_precio2, prd_precio3, prd_precio4) values(' + Edit5.Text + ', ''' + Edit1.Text
        + ''', ''' + Edit2.Text + ''', (select fmp_cdg from familia_producto where fmp_nombre = '''
        + DBComboBox1.Text + '''), ' + '(select fab_cdg from fabricante where fab_nombre = '''
        + DBComboBox2.Text + '''), ' + Edit3.Text + ', ' + Edit6.Text + ', ' + Edit7.Text + ', '
        + Edit4.Text + ', ' + Edit9.Text + ', ' + Edit10.Text + ', ' + Edit11.Text + ', '
        + Edit12.Text + ')';
        
      
    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
    
    SQLQuery3.SQL.Text := 'select max(prd_cdg) from producto where prd_nombre = '''
      + Edit1.Text + '''';
      
    SQLQuery3.Open;
    
    while not SQLQuery3.EOF do
    begin
      codigo := SQLQuery3.Fields[0].AsString;
      SQLQuery3.Next;
    end;
    
    SQLQuery3.Close;
    
    QueryString := 'select alta_arqueo(' + codigo + ', ' + Edit8.Text + ', '
      + registro.codigo_sucursal + ')';
    
    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');
    
    nombre := Edit1.Text;
    precio1 := Edit3.Text;
    precio2 := Edit10.Text;
    precio3 := Edit11.Text;
    precio4 := Edit12.Text;

    if MessageDlg('Alta Exitosa', 'Alta Exitosa. ¿Agregar otro producto?',
      mtConfirmation, [mbYes, mbCancel], 0) = mrYes then
      FormShow
    else
      Close;
  end
  else
    ShowMessage('ERROR EN EL INGRESO DE DATOS');
end;

procedure TForm12.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  if SQLQuery2.Active = True then
    SQLQuery2.Close;
    
  if SQLQuery3.Active = True then
    SQLQuery3.Close;
end;

initialization
  {$I unit12.lrs}

end.

